###########################################################
# $Id: xrsh_opts.sh 4684 2013-01-07 19:13:52Z xrdk $
# brief	    : 
# author    : xrdk
# version   : V1.0, 2004-03-16
#
# CopyRight (C) 2004-2011 xrdk.
# All rights reserved.
###########################################################

function xrmk_help()
{
	echo
	echo -----------usage----------------------------
	echo
	echo $0 [-c -t -r -k:info -m -g -s -D:def -H:dir -N:name -T:tgt -S:spec -C:cfg -h]
	echo
  echo -c clean
  echo -t create dirs for compile
  echo -r rebuild
  echo -k check compile env.
  echo
  echo -m compile in 64 bit, default in 32 bit
  echo -g compile in debug mode, default in release
  echo -s compile static lib, default is dso
  echo
  echo -D define compiler flags, can use many times
  echo -H set compiler home
  echo -N set out os name
  echo -C set out name config
  echo
	xrmk_scc_supped "get"
	echo
	xrmk_target_supped "get"
	echo
	xrmk_chkenv_supped "get"
	echo
  echo -h help
  echo --------------------------------------------
}

function xrmk_opts_parse()
{
	while getopts ":S:T:ctrk:mgsD:H:N:C:h" opt; do
	  echo Option: $opt $OPTARG
	  case $opt in
	    S)  xrmk_spec_cc=$OPTARG ;;
	    T)  xrmk_target=$OPTARG  ;;
	    c)  xrmk_cmd=clean    ;;
	    t)  xrmk_cmd=cdir     ;;
	    r)  xrmk_cmd=rebuild  ;;
	    k)  xrmk_cmd=check; xrmk_check_arg=$OPTARG ;;
	    m)  xrmk_64=yes       ;;
	    g)  xrmk_debug=yes    ;;
	    s)  xrmk_static=yes   ;;
	    D)  xrmk_defs="$xrmk_defs -D$OPTARG" ;;
	    H)  xrmk_cc_home="$OPTARG" ;;
	    N)  xrmk_specname="$OPTARG" ;;
	    C)  xrmk_cfg_usr="$OPTARG" ;;
	    h)
	      xrmk_help
	      exit 0
	      ;;
	    :)
	      echo "Option -$OPTARG requires an argument."
	      exit 1
	      ;;
	    ?)
	      echo "Unknown option"
	      exit 1
	      ;;
	  esac
	done
}
#=====================================================================
function xrmk_chkenv_supped()
{
	local _xrmk_arr=(a h o c m)
	
	if [ "$1" = "get" ]; then
	  echo -k env. check supped: ${_xrmk_arr[*]}
	  echo "--    h : host info."
	  echo "--    o : os info."
	  echo "--    c : compiler info."
	  echo "--    m : config info."
	  echo "--    a : all info."
		return
	fi
	
	local _xrmk_tmp=`echo ${_xrmk_arr[*]}`
	_xrmk_tmp=`xrsh_hasitem "$_xrmk_tmp" $1`
	
	if test "$_xrmk_tmp" = "no"; then
		echo "Unsupp env. check: $1"
		xrmk_chkenv_supped "get"
		exit 1
	fi
}

function xrmk_opts_valid()
{
	if [ "$xrmk_spec_cc" != "" ] && [ "$xrmk_target" != "" ]; then
		echo Option: -S and -T only use one same time
		exit 1
	fi
	
	if [ "$xrmk_spec_cc" != "" ]; then
		xrmk_scc_supped "$xrmk_spec_cc"
	fi
	
	if [ "$xrmk_target" != "" ]; then
		xrmk_target_supped "$xrmk_target"
	fi
	
	if [ "$xrmk_cmd" = "check" ]; then
		xrmk_chkenv_supped "$xrmk_check_arg"
	fi
}
#=====================================================================

function xrmk_chkenv_echo()
{
	if [ "$1" = "h" ]; then
		xrmk_host_echo
	elif [ "$1" = "c" ]; then
		xrmk_cc_echo
	elif [ "$1" = "o" ]; then
		xrmk_os_echo
	elif [ "$1" = "m" ]; then
		xrmk_config_echo
	elif [ "$1" = "a" ]; then
		xrmk_host_echo
		xrmk_os_echo
		xrmk_cc_echo
		xrmk_config_echo
	fi
}

function xrmk_opts_cmd()
{
	if [ "$xrmk_cmd" = "check" ]; then
		xrmk_chkenv_echo "$xrmk_check_arg"
		exit 0
	fi
}

